<?php
/**
 * [WeEngine System] Copyright (c) 2013 WE7.CC
 * User: fanyk
 * Date: 2018/2/6
 * Time: 10:18
 */

namespace Ken;


class Arrays extends \ArrayObject {

	private $input = array();
	public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator") {
		parent::__construct($input, $flags, $iterator_class);
		$this->input = $input;
	}

	public static function from(array $input) {
		return new Arrays($input, \ArrayObject::ARRAY_AS_PROPS);
	}

	/**
	 * map
	 * @param \Closure $closure
	 * @return Arrays
	 */
	public function map(\Closure $closure) {
		return static::from(array_map($closure, $this->input));
	}

	/**
	 *  过滤
	 * @param \Closure $closure
	 * @return Arrays
	 */
	public function filter(\Closure $closure) {
		return static::from(array_filter($this->input, $closure));
	}

	public function reduce(\Closure $closure, $initial = null) {
		return array_reduce($this->input, $closure, $initial);
	}

	/**
	 *  迭代数组
	 * @param \Closure $closure
	 * @return $this
	 */
	public function walk(\Closure $closure) {
		array_walk($this->input, $closure);
		return $this;
	}

	/**
	 * @return Arrays
	 */
	public function keys() {
		return static::from(array_keys($this->input));
	}

	/**
	 * @param $size
	 * @param $preserve_keys 是否保留key 值
	 */
	public function chunk($size, $preserve_keys = FALSE) {
		return static::from(array_chunk($this->input, $size, $preserve_keys));
	}

	/** 交换数组key value
	 * @return Arrays
	 */
	public function flip() {
		return static::from(array_flip($this->input));
	}

	/**
	 *  获取总和
	 * @return float|int
	 */
	public function sum() {
		return array_sum($this->input);
	}

	/**
	 *  合并数组
	 * @param array $array
	 * @return Arrays
	 */
	public function merge($array) {
		$arrays = func_get_args();
		$array = call_user_func_array('array_merge', $arrays);
		return static::from($array);
	}

	public function values() {
		return static::from(array_values($this->input));
	}

	public function toArray() {
		return $this->input;
	}



}